package com.dm.testkotal.fragment

import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.dm.testkotal.AbsAdapter
import com.dm.testkotal.R
import com.dm.testkotal.bean.Hello
import kotlinx.android.synthetic.main.fragment_blank.*
import org.jetbrains.anko.onItemClick
import org.jetbrains.anko.support.v4.toast
import java.util.*


class BlankFragment : Fragment() {

    private val list= ArrayList<Hello>()
    private var mListener: OnFragmentInteractionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater!!.inflate(R.layout.fragment_blank, container, false)
    }

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        if (context is OnFragmentInteractionListener) {
            mListener = context as OnFragmentInteractionListener?
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        loadData();

        val adapter=object :AbsAdapter<Hello>(context, R.layout.item, list) {
            override fun bindView(vHolder: AbsAdapter.ViewHolder, data: Hello) {
                val textHello=vHolder.getView(R.id.text1) as TextView
                val textTitile=vHolder.getView(R.id.text2) as TextView
                textHello.text=data.hello
                textTitile.text=data.text;
                Log.d("AAAAA","======================"+data.hello);
            }
        }
        listView.adapter=adapter


        listView.onItemClick { adapterView, view, i, l ->
            Log.d("AAAAA","======================"+i)
            toast(list.get(i).text)
            list.add(Hello("AAAA","Hu"))
            adapter.notifyDataSetChanged()
        }


    }

    private fun loadData() {
        list.add(Hello("122", "333"))
        list.add(Hello("3333", "8888"))
        Log.d("AAAAA","======================"+list.size);
    }

    override fun onDetach() {
        super.onDetach()
        mListener = null
    }


    interface OnFragmentInteractionListener {
        fun onFragmentInteraction(uri: Uri)
    }

}// Required empty public constructor
